package com.ruoyi.purchase.controller;

import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.purchase.domain.OrderDetail;
import com.ruoyi.purchase.service.IOrderDetailService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletResponse;
import java.util.List;

/**
 * 订单详情Controller
 *
 * @author kevinness
 * @date 2025-01-03
 */
@RestController
@RequestMapping("/orderDetail")
public class OrderDetailController extends BaseController
{
    @Autowired
    private IOrderDetailService orderDetailService;

    /**
     * 查询订单详情列表
     */
    @GetMapping("/list")
    public TableDataInfo list(OrderDetail OrderDetail)
    {
        startPage();
        List<OrderDetail> list = orderDetailService.selectOrderDetailList(OrderDetail);
        return getDataTable(list);
    }

    /**
     * 导出订单详情列表
     */
    @Log(title = "订单详情", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, OrderDetail OrderDetail)
    {
        List<OrderDetail> list = orderDetailService.selectOrderDetailList(OrderDetail);
        ExcelUtil<OrderDetail> util = new ExcelUtil<OrderDetail>(OrderDetail.class);
        util.exportExcel(response, list, "订单详情数据");
    }

    /**
     * 获取订单详情详细信息
     */
    @GetMapping(value = "/{orderNo}")
    public AjaxResult getInfo(@PathVariable("orderNo") String orderNo)
    {
        return success(orderDetailService.selectOrderDetailByOrderNo(orderNo));
    }

    /**
     * 根据订单编号获取货品详细购买信息
     */
    @PostMapping("/change/orderDetail")
    public TableDataInfo getOrderDetail(@RequestBody String orderNo)
    {
        startPage();
        OrderDetail OrderDetail = new OrderDetail();
        OrderDetail.setOrderNo(orderNo);
        List<OrderDetail> list = orderDetailService.selectOrderDetailList(OrderDetail);
        return getDataTable(list);
    }

    /**
     * 新增订单详情
     */
    @Log(title = "订单详情", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody OrderDetail OrderDetail)
    {
        return toAjax(orderDetailService.insertOrderDetail(OrderDetail));
    }

    /**
     * 修改订单详情列表
     */
    @Log(title = "订单详情", businessType = BusinessType.UPDATE)
    @PutMapping("/operator")
    public AjaxResult edit(@RequestBody List<OrderDetail> orderDetails)
    {
        return toAjax(orderDetailService.updateOrderDetailList(orderDetails));
    }

    /**
     * 删除订单详情
     */
    @Log(title = "订单详情", businessType = BusinessType.DELETE)
    @DeleteMapping("/{orderNos}")
    public AjaxResult remove(@PathVariable String[] orderNos)
    {
        return toAjax(orderDetailService.deleteOrderDetailByOrderNos(orderNos));
    }
}